package com.zhaoxi.project.sys.controller;

import com.zhaoxi.framework.aspectj.lang.annotation.Log;
import com.zhaoxi.framework.aspectj.lang.enums.BusinessType;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.project.sys.service.AttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 附件Controller
 *
 * @author zhaoxi
 * @date 2023-11-09
 */
@RestController
@RequestMapping("/sys/attachment")
public class AttachmentController extends BaseService {
  @Autowired
  private AttachmentService attachmentService;

  //  获列表
  @PreAuthorize("@ss.hasPermi('sys:attachment:list')")
  @GetMapping("/getList")
  public PageData getList(Map map) {
    PageData pd = this.getPageData(map);
    //如果 pageNum 和 pageSize 都是有值的，就是需要分页的
    if (pd.getLong("pageNum") != null && pd.getLong("pageSize") != null) {
      this.startPage();
    }
    return this.responses(attachmentService.getList(pd));
  }

  //  获列表
  @PreAuthorize("@ss.hasPermi('sys:attachment:list')")
  @GetMapping("/getInfo")
  public PageData getInfo(Map map) {
    PageData pd = this.getPageData(map);
    return attachmentService.getById(pd);
  }

  //  新增
  @PreAuthorize("@ss.hasPermi('sys:attachment:add')")
  @Log(title = "样例管理", businessType = BusinessType.INSERT)
  @PostMapping
  public PageData add(@RequestBody Map map) {
    PageData pd = this.getPageData(map);
    return attachmentService.add(pd);
  }

  //  修改
  @PreAuthorize("@ss.hasPermi('sys:attachment:edit')")
  @Log(title = "样例管理", businessType = BusinessType.UPDATE)
  @PutMapping
  public PageData edit(@RequestBody Map map) {
    PageData pd = this.getPageData(map);
    return attachmentService.edit(pd);
  }

  //  删除
  @PreAuthorize("@ss.hasPermi('sys:attachment:del')")
  @Log(title = "样例管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/{id}")
  public PageData del(@PathVariable Long id) {
    PageData pd = new PageData();
    pd.put("id", id);
    pd.put("tenantId",this.getTenantId());
    try {
      return attachmentService.del(pd);
    } catch (IOException e) {
      return PageData.error(e.getMessage());
    }
  }

  /**
   * 删除附件
   */
  @PreAuthorize("@ss.hasPermi('sys:attachment:delete')")
  @Log(title = "附件", businessType = BusinessType.DELETE)
  @DeleteMapping("/delUrl")
  public PageData delUrl(String fileUrl) {
    try {
      attachmentService.delByUrl(fileUrl);
      return success("删除成功");
    } catch (Exception e) {
      return PageData.error(e.getMessage());
    }
  }

  /**
   * 新增多个附件
   */
  @PreAuthorize("@ss.hasPermi('sys:attachment:moreEdit')")
  @Log(title = "附件", businessType = BusinessType.UPDATE)
  @PostMapping("/moreAdd")
  public PageData moreAdd(@RequestBody Map map) {
    PageData pd = this.getPageData(map);

    return attachmentService.moreAdd(pd);
  }


  /**
   * 附件上传请求（多个）
   */
  @PreAuthorize("@ss.hasPermi('sys:attachment:picture')")
  @Log(title = "图片上传", businessType = BusinessType.UPDATE)
  @PostMapping("/uploadPicture")
  public PageData uploadFile(List<MultipartFile> filelist) {
    try {
      return attachmentService.uploadAttachment(filelist);
    } catch (Exception e) {
      return PageData.error(e.getMessage());
    }
//
  }
  /**
   * 删除附件
   */
  @PreAuthorize("@ss.hasPermi('sys:attachment:delete')")
  @Log(title = "附件", businessType = BusinessType.DELETE)
  @DeleteMapping("/delFile")
  public PageData delFile(String fileUrl) {
    try {
      attachmentService.delFile(fileUrl);
      return success("删除成功");
    } catch (Exception e) {
      return PageData.error(e.getMessage());
    }
  }



}
